// 连接数据库
const { Sequelize, Model, DataTypes } = require("sequelize");

const mysql = {
  host: "127.0.0.1",
  user: "root",
  password: "root",
  database: "express",
  port: 3306,
};

// 创建连接
const sequelizeConnection = new Sequelize(
  mysql.database,
  mysql.user,
  mysql.password,
  {
    host: mysql.host,
    dialect: "mysql",
    logging: (msg) => {
      console.log(msg);
    },
    define: {
      timestamps: true,
      freezeTableName: true,
      paranoid: false,
    },
    // 不清除数据库中的数据
    dropSchema: true,
    pool: {
      max: 5,
      min: 0, 
      acquire: 30000,
      idle: 10000,
    },
    timezone: "+08:00",
    dialectOptions: {
      dateStrings: true,
    },
    // 设置重连
    retry: {
      max: 3,
    },
  }
);

// 监听数据库创建
sequelizeConnection
  .authenticate({
    raw: true,
  })
  .then(() => {
    console.log("数据库连接成功");
  })
  .catch((err) => {
    // console.error("Unable to connect to the database:", err);
    console.table(err);
  });

sequelizeConnection.sync({ force: false });

module.exports = {
  sequelizeConnection,
  Model,
  DataTypes,
};
